package spring.superrun.record.mvc.controller;

import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Length;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import spring.superrun.record.mvc.dto.ManagerAddDto;
import spring.superrun.record.mvc.dto.ManagerUpdateDto;
import spring.superrun.record.mvc.entity.SuperrunManager;
import spring.superrun.record.mvc.entity.enumlibs.ServiceResultCode;
import spring.superrun.record.mvc.entity.response.ServiceResult;
import spring.superrun.record.mvc.entity.response.ServiceResultOfT;
import spring.superrun.record.mvc.service.SuperrunManagerService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * (SuperrunManager)表控制层
 *
 * @author makejava
 * @since 2021-06-15 21:16:43
 */
@RestController
@RequestMapping("manager")
@ApiOperation("管理员控制器")
public class ManagerController {
    /**
     * 服务对象
     */
    @Resource
    private SuperrunManagerService superrunManagerService;
    @GetMapping
    @ApiOperation("获取所有管理员基本信息")
    public ResponseEntity<ServiceResultOfT<List<SuperrunManager>>> GetManagers(){
        ServiceResultOfT<List<SuperrunManager>> result =
                new ServiceResultOfT<List<SuperrunManager>>();
        result.IsFailed(ServiceResultCode.NotFound,"未找到符合条件的管理员信息");
        List<SuperrunManager> entities =superrunManagerService
                .queryAllByLimit(0,100);
        result.setEntities(entities);
        if (result.getEntities()==null){
            return new ResponseEntity<>(result, HttpStatus.NOT_FOUND);
        }
        result.IsSuccess("成功获取到所有管理员信息");
        return new ResponseEntity<>(result, HttpStatus.OK);
    }

    /**
     *通过id查询一个管理员信息
     * @return ResponseEntity<ServiceResultOfT<SuperrunManager>>
     */
    @GetMapping(value = "/{id}")
    @ApiOperation("获取某个管理员信息根据id")
    public  ResponseEntity<ServiceResultOfT<SuperrunManager>> GetManager(@PathVariable String id){
        ServiceResultOfT<SuperrunManager> result = new ServiceResultOfT<>();
        result.IsFailed(ServiceResultCode.NotFound,"未找到符合条件的管理员信息");
        if (id==null||id.isEmpty()){
            result.IsFailed(ServiceResultCode.KeyIsNull,"为输入关键信息");
            return  new ResponseEntity<>(result,HttpStatus.BAD_REQUEST);
        }
        result.setEntities(superrunManagerService.queryById(id));
        if (result.getEntities()==null){
            return new ResponseEntity<>(result,HttpStatus.NOT_FOUND);
        }
        result.IsSuccess("查询一个管理员信息成功");
        return  new ResponseEntity<>(result,HttpStatus.OK);
    }
    @PostMapping
    @ApiOperation("添加一个管理员信息")
    public ResponseEntity<ServiceResult> AddManager(@RequestBody @Valid ManagerAddDto dto){
        ServiceResult result = new ServiceResult();
        result.IsFailed("添加失败");
        if (dto==null){
            result.IsFailed(ServiceResultCode.KeyIsNull,"位置关键信息为空,添加失败");
            return new ResponseEntity<>(result,HttpStatus.BAD_REQUEST);
        }
        SuperrunManager Manager = new SuperrunManager();
        SuperrunManager ManagerAdd = superrunManagerService.insert(Manager.Add(dto));
        if (ManagerAdd==null){
            return  new ResponseEntity<>(result,HttpStatus.BAD_REQUEST);
        }
        result.IsSuccess("添加一个管理员信息成功");
        return  new ResponseEntity<>(result,HttpStatus.OK);
    }
    @PutMapping
    @ApiOperation("更新一个管理员信息")
    public ResponseEntity<ServiceResult> UpdateManager(@RequestBody @Valid ManagerUpdateDto dto){
        ServiceResult result = new ServiceResult();
        result.IsFailed("更新失败");
        if (dto.getId()==null||dto.getId().isEmpty()){
            result.IsFailed(ServiceResultCode.KeyIsNull,"为输入关键信息");
            return  new ResponseEntity<>(result,HttpStatus.BAD_REQUEST);
        }
        SuperrunManager Manager = superrunManagerService.queryById(dto.getId());
        if (Manager==null){
            return  new ResponseEntity<>(result,HttpStatus.BAD_REQUEST);
        }
        SuperrunManager locationUpdate= superrunManagerService.update(Manager.Update(dto));
        if (locationUpdate==null){
            return  new ResponseEntity<>(result,HttpStatus.BAD_REQUEST);
        }
        result.IsSuccess("更新成功");
        return new ResponseEntity<>(result,HttpStatus.OK);
    }
    @DeleteMapping(value = "/{id}")
    @ApiOperation("删除一个管理员信息")
    public ResponseEntity<ServiceResult> DelManager
            (@PathVariable @Length(min = 20,max = 50) String id){
        ServiceResult result = new ServiceResult();
        result.IsFailed("删除失败");
        if (id==null||id.isEmpty()){
            result.IsFailed(ServiceResultCode.KeyIsNull,"为输入关键信息");
            return  new ResponseEntity<>(result,HttpStatus.BAD_REQUEST);
        }
        boolean isDel = superrunManagerService.deleteById(id);
        if (!isDel){
            return  new ResponseEntity<>(result,HttpStatus.BAD_REQUEST);
        }
        result.IsSuccess("删除成功");
        return new ResponseEntity<>(result,HttpStatus.OK);
    }
}
